<script setup>
import {nextTick, ref} from 'vue'
import {addRoleService, deleteRoleService, roleListService, updateRoleService} from "@/api/role.js";
import {ElMessage, ElMessageBox} from "element-plus";
import {limitListService} from "@/api/limit.js";

const roleList = ref([])
const getInit = async () => {
  roleList.value = await roleListService()//获取角色列表
}
const addVisible = ref(false)
const limitVisible = ref(false)
const treeRef = ref()
const editVisible = ref(false)
const role = ref({})
const addRole = async () => {
  await addRoleService(role.value)
  addVisible.value = false
  await getInit()
  ElMessage.success('添加成功')
}
const removeRole = async (id) => {
  await ElMessageBox.confirm('此操作将永久删除该文件, 是否继续?', '提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }
  ).then(async () => {
    await deleteRoleService(id)
    await getInit()
    ElMessage.success('删除成功')
  })
}
const limitList = ref([])
const getLimit = async (tree) => {
  limitList.value = await limitListService()
<<<<<<< HEAD
  // treeRef.value.setCheckedNodes([112])
  addArray(limitList.value)
  console.log(arr.value)
=======
   console.log(limitList.value)
  //treeRef.value.setCheckedNodes([105])
  //treeRef.value.setCheckedKeys(row.rights)
  console.log(treeRef.value)
>>>>>>> 6ce0f859b4e893dc690ff84a1db458a15cf0aabd
  limitVisible.value = true
}


const editRole = (row) => {
  editVisible.value = true
  role.value = row
}
const updateRole = async () => {
  await updateRoleService(role.value.id, role.value)
  editVisible.value = false
  await getInit()
  ElMessage.success('修改成功')
}
const arr = ref([])

const addArray = (list) => {
  list.forEach(item => {
    if (item.children !== undefined) {
      addArray(list.children)
    }
    arr.value.push(list.id)
  })
}

getInit()
</script>
<template>
  <el-card>
    <template #header>
      <div class="header">
        <h2>角色列表</h2>
        <el-button type="primary" @click="addVisible=true">添加角色</el-button>
      </div>
    </template>
    <el-table :data="roleList">
      <el-table-column label="序号" type="index" width="80"></el-table-column>
      <el-table-column label="角色名称" prop="roleName"></el-table-column>
      <el-table-column label="描述" prop="roleDesc"></el-table-column>
      <el-table-column label="操作">
        <template #default="{row}">
          <el-button type="primary" @click="getLimit">权限编辑</el-button>
          <el-button type="primary" @click="editRole(row)">信息编辑</el-button>
          <el-button type="danger" @click="removeRole(row.id)">删除角色</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <el-dialog title="添加角色" v-model="addVisible" width="500">
    <el-form v-model="role">
      <el-form-item label="名称">
        <el-input v-model="role.roleName"></el-input>
      </el-form-item>
      <el-form-item label="描述">
        <el-input v-model="role.roleDesc"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addRole">确定</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
  <el-dialog title="编辑权限" v-model="limitVisible" width="500">
    <el-tree
        ref="treeRef"
        style="max-width: 600px"
        :data="limitList"
        :props="{
        label: 'authName',
        children: 'children'
      }"
        node-key="id"
        :default-checked-keys="arr"
        show-checkbox
    />
    <el-button type="primary">提交修改</el-button>
  </el-dialog>
  <el-dialog title="编辑角色" v-model="editVisible" width="500">
    <el-form v-model="role">
      <el-form-item label="名称">
        <el-input v-model="role.roleName"></el-input>
      </el-form-item>
      <el-form-item label="描述">
        <el-input v-model="role.roleDesc"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="updateRole">确定</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<style lang="scss" scoped>
.el-card {
  .header {
    display: flex;
    justify-content: space-between;
  }
}
</style>
